package com.by.service;

import com.by.mapper.OrderSettingMapper;
import com.by.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public void addOrderSetting(List<String[]> stringList) {
        for (String[] stringArr : stringList) {
            //2023/12/06
            String orderDate = stringArr[0];
            String number = stringArr[1];
            addOrderSetting(orderDate, number);
        }
    }

    /**
     * @Description:判断当前日期是否设置过，如果设置过，删除
     * @Param: * @param orderDate
     * @return: void
     * @Author: GP
     * @Date: 2024/1/12
     */
    private void addOrderSetting(String orderDate, String number) {
        //1、判断当前日期是否设置过可预约人数，如果过设置过则删除
        int orderSettingCount = orderSettingMapper.getCountOfOrderSettingByOrderDate(orderDate);
        if (orderSettingCount > 0) {
            orderSettingMapper.deleteOrderSettingByOrderDate(orderDate);
        }
        orderSettingMapper.addOrderSetting(orderDate, number);
    }

    @Override
    public List<Map<String, Object>> getOrderSettingByMonth(String date) {
        /*
          [
            {id:76,orderDate:2023-11-23,number:107,reservations:O},
            {id:75,orderDate:2023-11-23,number:107,reservations:O},
            {id:74,orderDate:2023-11-23,number:107,reservations:O}
          ]
        */
        List<OrderSetting> orderSettingList = orderSettingMapper.getOrderSettingByMonth(date);

        List<Map<String, Object>> mapList = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            Map<String, Object> map = new HashMap<>();
            map.put("date", orderSetting.getOrderDate().getDate());
            map.put("number", orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations());
            mapList.add(map);
        }
        return mapList;
    }

    /**
    * @Description: 点击设置来设置预约人数
            * @Param: * @param orderSetting
            * @return: void
            * @Author: GP
            * @Date: 2024/1/12
            */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/M/d");
        String dateStr = sdf.format(orderSetting.getOrderDate());
        //valueOf(int i): 返回 int 参数的字符串表示形式。
        addOrderSetting(dateStr, String.valueOf(orderSetting.getNumber()));
    }

//    public static void main(String[] args) {
//        /*
//        * 数组：内存中挨着分配的一些小格子，每个小格子都有一个下标
//        * [a] [b] [c] [d]
//        *  0   1   2   3
//        * */
//
//        String[] stringArr = new String[]{"a", "b", "c", "d"};
//        System.out.println(stringArr[0]);
//        System.out.println(stringArr[2]);
//
//        Date date = new Date();
//        System.out.println(date);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/M/d");
//        String dateStr = sdf.format(date);
//        System.out.println(dateStr);
//    }

}
